package com.xckj.cycling.utils

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.xckj.cycling.context.AppContext

object StatusBarHelper {
    /**
     * 获取系统状态栏高度
     *
     * @param context
     * @return
     */
    @JvmStatic
    fun getStatusbarHeight(context: Context?): Int {
        var context = context
        if (context == null) {
            context = AppContext.globalContext()
        }
        var result = 0
        val resourceId = context!!.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }
    /**
     * 获取系统状态栏高度
     *
     * @param context
     * @return
     */
    @JvmStatic
    fun getStatusbarHeightInDp(): Int {
        var context = AppContext.globalContext()
        var result = 0
        val resourceId = context!!.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return (result/context.resources.displayMetrics.density).toInt()
    }

    @JvmStatic
    fun getActionBarHeight(context: Context): Int {
        val actionbarSizeTypedArray = context.obtainStyledAttributes(
            intArrayOf(
                androidx.appcompat.R.attr.actionBarSize
            )
        )
        val actionBarHeight = actionbarSizeTypedArray.getDimensionPixelSize(0, 0)
        actionbarSizeTypedArray.recycle()
        return actionBarHeight
    }

    fun getStatusbarHeightByApiLevel(context: Context?): Int {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getStatusbarHeight(context)
        } else 0
    }

    @JvmStatic
    fun showStatusBar(activity: Activity?, isBlack: Boolean) {
        if (activity == null) {
            return
        }
        val window = activity.window
        if (isBlack) {
            var uiFlags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window.decorView.systemUiVisibility = uiFlags
        } else {
            val uiFlags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
            window.decorView.systemUiVisibility = uiFlags
        }
    }

    @JvmStatic
    fun hideStatusBar(activity: Activity?) {
        if (activity == null) {
            return
        }
        val window = activity.window
        var uiFlags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            uiFlags = uiFlags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
        }
        window.decorView.systemUiVisibility = uiFlags
    }

    /**
     * 获取虚拟按键的高度
     * 1. 全面屏下
     * 1.1 开启全面屏开关-返回0
     * 1.2 关闭全面屏开关-执行非全面屏下处理方式
     * 2. 非全面屏下
     * 2.1 没有虚拟键-返回0
     * 2.1 虚拟键隐藏-返回0
     * 2.2 虚拟键存在且未隐藏-返回虚拟键实际高度
     */
    fun getNavigationBarHeightIfRoom(context: Context): Int {
        return if (navigationGestureEnabled(context)) {
            0
        } else getCurrentNavigationBarHeight(context as Activity)
    }

    /**
     * 全面屏（是否开启全面屏开关 0 关闭  1 开启）
     *
     * @param context
     * @return
     */
    fun navigationGestureEnabled(context: Context): Boolean {
        val `val` = Settings.Global.getInt(context.contentResolver, deviceInfo, 0)
        return `val` != 0
    }

    /**
     * 获取设备信息（目前支持几大主流的全面屏手机，亲测华为、小米、oppo、魅族、vivo都可以）
     *
     * @return
     */
    val deviceInfo: String
        get() {
            val brand = Build.BRAND
            if (TextUtils.isEmpty(brand)) return "navigationbar_is_min"
            return if (brand.equals("HUAWEI", ignoreCase = true)) {
                "navigationbar_is_min"
            } else if (brand.equals("XIAOMI", ignoreCase = true)) {
                "force_fsg_nav_bar"
            } else if (brand.equals("VIVO", ignoreCase = true)) {
                "navigation_gesture_on"
            } else if (brand.equals("OPPO", ignoreCase = true)) {
                "navigation_gesture_on"
            } else {
                "navigationbar_is_min"
            }
        }

    /**
     * 非全面屏下 虚拟键实际高度(隐藏后高度为0)
     * @param activity
     * @return
     */
    fun getCurrentNavigationBarHeight(activity: Activity): Int {
        return if (isNavigationBarShown(activity)) {
            getNavigationBarHeight(activity)
        } else {
            0
        }
    }

    /**
     * 非全面屏下 虚拟按键是否打开
     * @param activity
     * @return
     */
    @JvmStatic
    fun isNavigationBarShown(activity: Activity): Boolean {
        //虚拟键的view,为空或者不可见时是隐藏状态
        val view = activity.findViewById<View>(android.R.id.navigationBarBackground)
            ?: return false
        val visible = view.visibility
        return if (visible == View.GONE || visible == View.INVISIBLE) {
            false
        } else {
            true
        }
    }

    /**
     * 非全面屏下 虚拟键高度(无论是否隐藏)
     * @param context
     * @return
     */
    @JvmStatic
    fun getNavigationBarHeight(context: Context): Int {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        val dm = DisplayMetrics()
        try {
            val c = Class.forName("android.view.Display")
            val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)
            method.invoke(display, dm)
            return dm.heightPixels - display.height
        } catch (e: Exception) {
            e.printStackTrace()
        }
        var result = 0
        val resourceId =
            context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }



}